const db = require("../../lib/db");

//获取文章
exports.getArticle = (pageSize, nowPage, state, subsetId, searchTerm, classify) => {
  let _sql;
  let params = [];

  if (searchTerm) {
    _sql = `SELECT * FROM article WHERE concat(title,introduce) like ? and classify = ? order by id desc limit ?, ?`;
    params = [`%${searchTerm}%`, classify, (nowPage - 1) * pageSize, pageSize];
  } else if (subsetId > -1 && typeof subsetId == "number") {
    if (state === -1) {
      _sql = `SELECT * FROM article WHERE subset_id = ? and classify = ? order by id desc limit ?, ?`;
      params = [subsetId, classify, (nowPage - 1) * pageSize, pageSize];
    }else{
      _sql = `SELECT * FROM article WHERE subset_id = ? and classify = ? and state = ? order by id desc limit ?, ?`;
      params = [subsetId, classify, state, (nowPage - 1) * pageSize, pageSize];
    }
  } else if (typeof subsetId == "string") {
    // 处理字符串形式的多个ID
    const idArray = subsetId.split(',').map(id => id.trim()).filter(id => id);
    if (idArray.length > 0) {
      const placeholders = idArray.map(() => '?').join(',');
      if (state === -1) {
        _sql = `SELECT * FROM article WHERE subset_id not in (${placeholders}) and classify = ? order by id desc limit ?, ?`;
        params = [...idArray, classify, (nowPage - 1) * pageSize, pageSize];
      }else{
      _sql = `SELECT * FROM article WHERE subset_id not in (${placeholders}) and classify = ? and state = ? order by id desc limit ?, ?`;
      params = [...idArray, classify, state, (nowPage - 1) * pageSize, pageSize];
    }
    }
    else {
      _sql = `SELECT * FROM article WHERE classify = ? order by id desc limit ?, ?`;
      params = [classify, (nowPage - 1) * pageSize, pageSize];
    }
  } 
  else if (state > -1) {
    _sql = `SELECT * FROM article WHERE state = ? and classify = ? order by id desc limit ?, ?`;
    params = [state, classify, (nowPage - 1) * pageSize, pageSize];
  } else {
    _sql = `SELECT * FROM article WHERE classify = ? order by id desc limit ?, ?`;
    params = [classify, (nowPage - 1) * pageSize, pageSize];
  }
  return db.queryWithPool(_sql, params);
};

//获取文章总数
exports.getArticleCount = (state, subsetId, searchTerm, classify) => {
  let _sql;
  let params = [];

  if (searchTerm) {
    _sql = `SELECT count(*) AS count FROM article WHERE concat(title,introduce) like ? and classify = ?`;
    params = [`%${searchTerm}%`, classify];
  } else if (subsetId > -1 && typeof subsetId == "number") {
    if(state === -1){
      _sql = `SELECT count(*) AS count FROM article WHERE subset_id = ? and classify = ?`;
      params = [subsetId, classify];
    }else{
      _sql = `SELECT count(*) AS count FROM article WHERE subset_id = ? and classify = ? and state = ?`;
      params = [subsetId, classify, state];
    }
  }
  else if (typeof subsetId == "string" && state == -1) {
    const idArray = subsetId.split(',').map(id => id.trim()).filter(id => id);
    if (idArray.length > 0) {
      const placeholders = idArray.map(() => '?').join(',');
      _sql = `SELECT count(*) AS count FROM article WHERE subset_id not in (${placeholders}) and classify = ?`;
      params = [...idArray, classify];
    } else {
      _sql = `SELECT count(*) AS count FROM article WHERE classify = ?`;
      params = [classify];
    }
  } else if (state > -1) {
    _sql = `SELECT count(*) AS count FROM article WHERE state = ? and classify = ?`;
    params = [state, classify];
  } else {
    _sql = `SELECT count(*) AS count FROM article WHERE classify = ?`;
    params = [classify];
  }
  return db.queryWithPool(_sql, params);
};

//获取文章名称
exports.getArticTitle = (id) => {
  let _sql = `SELECT title FROM article WHERE id = ?`;
  return db.queryWithPool(_sql, [id]);
};

//通过id获取文章/图库
exports.getArticleById = (id) => {
  let _sql = `SELECT * FROM article WHERE id = ?`;
  return db.queryWithPool(_sql, [id]);
};

//新建文章/图库
exports.addArticle = (value) => {
  let _sql = `INSERT INTO article set ?`;
  return db.queryWithPool(_sql, value);
};

//修改文章/图库
exports.updateArticle = (id, value) => {
  let _sql = `UPDATE article SET ? WHERE id = ?`;
  return db.queryWithPool(_sql, [value, id]);
};

//更新文章状态
exports.updateArticleState = (id, state) => {
  let _sql = `UPDATE article SET state = ? WHERE id = ?`;
  return db.queryWithPool(_sql, [state, id]);
};

//删除文章
exports.deleteArticle = (id) => {
  let _sql = `DELETE FROM article WHERE id = ?`;
  return db.queryWithPool(_sql, [id]);
};

